package com.kingyea.mobilepolice.apimanage.requester.service.impl;

import com.kingyea.mobilepolice.apimanage.requester.bean.Requester;
import com.kingyea.mobilepolice.apimanage.requester.dao.RequesterMapper;
import com.kingyea.mobilepolice.apimanage.requester.service.RequesterService;
import com.kingyea.mobilepolice.apimanage.systemsync.Send2Queue;
import com.kingyea.mobilepolice.apimanage.utils.NumberUtils;
import com.kingyea.mobilepolice.apimanage.utils.StateCode;
import com.kingyea.mobilepolice.apimanage.utils.page.PageInfo;
import com.kingyea.mobilepolice.system.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Mr.Lin
 * @version v1.0.0
 * @description Requester 实现类
 * @date Created in 2018-02-23 14:55:28
 */
@Service
@Transactional
public class RequesterServiceImpl implements RequesterService {

    private static final Logger LOGGER = LoggerFactory.getLogger(RequesterServiceImpl.class);

    @Resource
    private RequesterMapper requesterMapper;

    /**
     * 通过ID查找对象
     *
     * @param id
     * @return
     */
    @Override
    public Requester getById(Integer id) throws Exception {
        return this.requesterMapper.getById(id);
    }

    /**
     * 列表
     *
     * @param
     * @return
     */
    @Override
    public List<Requester> list(Requester requester) throws Exception {
        List<Requester> list = requesterMapper.list(requester);
        return list;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Send2Queue
    @Override
    public int deleteById(Integer id) throws Exception {
        requesterMapper.deleteById(id);
        return 1;
    }

    /**
     * 保存
     *
     * @param requester
     * @return
     */
    @Send2Queue
    @Override
    public Requester save(Requester requester) throws Exception {
        //判空
        if (StringUtils.isEmpty(requester.getSerialNo()) || StringUtils.isEmpty(requester.getRequester())
                || StringUtils.isEmpty(requester.getRequestNetwork()) || StringUtils.isEmpty(requester.getOrganizationId())
                || StringUtils.isEmpty(requester.getOrganization()) || StringUtils.isEmpty(requester.getRequestMac())) {
            throw new BusinessException(StateCode.NO_ARGUMENT_ERROR);
        }

        //  唯一标识重复
        Requester isExist = requesterMapper.getBySerialNo(requester.getSerialNo());
        if (isExist != null) {
            throw new BusinessException(StateCode.UNIQUE_IDENTIFIER_EXIST);
        }
        requester.setCreateTime(new Date());
        requesterMapper.save(requester);
        return requester;
    }

    /**
     * 更新
     *
     * @param requester
     * @return
     */
    @Send2Queue
    @Override
    public Requester update(Requester requester) throws Exception {
        try {
            requester.setModifiedTime(new Date());
            requesterMapper.update(requester);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return requester;
    }

    /**
     * 模糊查询
     *
     * @param map
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<List<Requester>> searchByCondition(Map<String, Object> map) throws Exception {
        PageInfo.startPage(map);
        return PageInfo.getPageInfo(requesterMapper.searchByCondition(map));
    }

    @Send2Queue
    @Override
    public Object check(Map<String, Object> map) throws Exception {
        if (!map.containsKey("id") || !map.containsKey("status") || StringUtils.isEmpty(map.get("status")) || NumberUtils.isNullOrLEZero(map.get("id"))) {
            throw new BusinessException(StateCode.NO_ARGUMENT_ERROR);
        }
        String status = map.get("status").toString();
        if (!status.equals("3") && !status.equals("4")) {
            throw new BusinessException(StateCode.ILLEGAL_ARGUMENT_ERROR);
        }
        Requester paramObj = new Requester();
        paramObj.setId(NumberUtils.obj2Int(map.get("id")));
        paramObj.setStatus(NumberUtils.obj2Int(map.get("status")));
        paramObj.setAuditTime(new Date());
        paramObj.setModifiedTime(new Date());
        paramObj.setModifiedById(String.valueOf(map.get("userId")));
        requesterMapper.update(paramObj);
        return 1;
    }

    /**
     * 带条件统计请求方数量
     *
     * @param map
     * @return java.lang.Object
     * @author Mr.Lin
     * @date 2018/3/23 10:40
     */
    @Override
    public Integer countByCondition(Map<String, Object> map) {
        return requesterMapper.countByCondition(map);
    }

    @Override
    public Object requesterCountByOrganization(Map<String, Object> map)
            throws Exception {
        PageInfo.startPage(map);
        return PageInfo.getPageInfo(requesterMapper.requesterCountByOrganization(map));
    }

    @Override
    public Object monthlyRequesterCount(Map<String, Object> map) throws Exception {
        PageInfo.startPage(map);
        return PageInfo.getPageInfo(requesterMapper.monthlyRequesterCount(map));
    }


}
